package com.ideal.controller;

import com.ideal.dto.Page;
import com.ideal.dto.ResultDto;
import com.ideal.dto.config.req.VersionPageReqDto;
import com.ideal.dto.config.res.VersionCompareDataRsqDto;
import com.ideal.model.ConfigVersion;
import com.ideal.service.IAggregateConfigService;
import com.ideal.service.IConfigVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 配置版本 Controller
 *
 * @author songfz
 * @date 2022-07-13
 */
@Controller
@RequestMapping("api/config/version")
public class ConfigVersionController
{
    @Autowired
    private IConfigVersionService configVersionService;

    @Autowired
    private IAggregateConfigService aggregateConfigService;

    /**
     * 查询配置版本 列表
     */
    @RequestMapping(value = "/page",method = RequestMethod.POST)
    @ResponseBody
    public ResultDto list(@RequestBody VersionPageReqDto reqDto)
    {
        return ResultDto.success(configVersionService.versionPage(reqDto));
    }

    /**
     * 导出配置版本 列表
     */
    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @ResponseBody
    public void export(HttpServletResponse response, String version)
    {

    }

    /**
     * 获取配置版本 详细信息
     */
    @RequestMapping(value = "/detail/{versionId}",method = RequestMethod.GET)
    @ResponseBody
    public ResultDto getInfo(@PathVariable("versionId") Long versionId)
    {
        return ResultDto.success(configVersionService.selectConfigVersionById(versionId));
    }

    /**
     * 配置文件下载
     * @param response
     * @param versionId
     */
    @RequestMapping(value = "/download/{versionId}",method = RequestMethod.GET)
    public void download(HttpServletResponse response,@PathVariable("versionId") Long versionId){
        configVersionService.downloadByConfigId(response,versionId);
    }

    /**
     * 配置对比历史数据
     * @param versionIds
     * @return
     */
    @RequestMapping(value = "compare/data/{versionIds}",method = RequestMethod.GET)
    @ResponseBody
    public ResultDto compareData(@PathVariable Long [] versionIds){
        List<VersionCompareDataRsqDto> data = configVersionService.compareData(versionIds);
    if (data.size() == 2 ) {

        return ResultDto.success(data);
    }else {
        return ResultDto.fail("数据异常");
    }
}
}
